package com.linkoog.devtools.http.request;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Splitter;
import com.intellij.openapi.util.Disposer;
import com.intellij.ui.JBColor;
import com.intellij.ui.JBSplitter;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.components.BorderLayoutPanel;
import com.linkoog.devtools.common.BizException;
import com.linkoog.devtools.common.Pair;
import com.linkoog.devtools.http.client.HttpClient;
import com.linkoog.devtools.http.raw_index.HttpServiceMethod;
import com.linkoog.devtools.http.request.common.HttpRequest;
import com.linkoog.devtools.http.request.common.HttpResponse;
import com.linkoog.devtools.http.request.common.RequestBody;
import com.linkoog.devtools.http.request.http_request.HttpServiceTestRequestPanel;
import com.linkoog.devtools.http.request.http_response.HttpServiceTestResponsePanel;
import com.linkoog.devtools.http.request.http_send.HttpServiceTestSendPanel;
import com.linkoog.devtools.utils.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class HttpServiceTestPanel extends BorderLayoutPanel implements Disposable {

    private final Project project;

    private HttpServiceTestSendPanel sendPanel;
    private HttpServiceTestRequestPanel requestPanel;

    private HttpServiceTestResponsePanel responsePanel;

    public HttpServiceTestPanel(Project project) {


        setBorder(JBUI.Borders.customLine(JBColor.LIGHT_GRAY));
        this.project = project;
        sendPanel = new HttpServiceTestSendPanel();
        Disposer.register(this, sendPanel);
        requestPanel = new HttpServiceTestRequestPanel(project);
        Disposer.register(this, requestPanel);
        responsePanel = new HttpServiceTestResponsePanel(project);
        Disposer.register(this, responsePanel);

        // 左右分割面板
        Splitter splitter = new Splitter(false, 0.5F);
        //splitter.setResizeEnabled(false);
        splitter.setDividerPositionStrategy(Splitter.DividerPositionStrategy.KEEP_FIRST_SIZE);
        splitter.setFirstComponent(requestPanel);
        splitter.setSecondComponent(responsePanel);

        this.add(sendPanel, BorderLayout.NORTH);
        this.add(splitter, BorderLayout.CENTER);

        initSendButton();
    }

    private void initSendButton() {
        HttpClient httpClient = new HttpClient();
        sendPanel.getSendButton().addActionListener(e -> {
            HttpRequest httpRequest = buildHttpRequest();
            HttpResponse response = httpClient.send(httpRequest);
            responsePanel.reset(response);
        });
    }

    private HttpRequest buildHttpRequest(){
        HttpRequest.HttpRequestBuilder builder = HttpRequest.builder();
        builder.httpMethod(sendPanel.getHttpMethodLabel().getHttpMethod());
        builder.url(sendPanel.getUrlTextField().getText());
        builder.params(requestPanel.getParams());
        builder.headers(requestPanel.getHeaders());
        builder.body(requestPanel.getBody());
        return builder.build();
    }


    public void resetPanel(HttpServiceMethod httpServiceMethod) {
    }

    @Override
    public void dispose() {
    }
}
